/*
 * @Description: 缓存一些src
 */
import { defineStore } from 'pinia';
import pinia from '@/store';
import globalData from '@/plugins/data';
import globalMethods from '@/plugins/methods';
import { useBaseInfoStore,getAccessFileSecretLevel } from './base-info';
import summary from "@/handles/handle-list/summary";

export const useSrcCacheStore = defineStore('srcCache', {
    state: () => ({
        workflowUrl: '', //工作流
        param:{//工作流参数
            SD:'',
            CWID:'',
            currentNodeId:'',
        },//参数变化
        changed:false,
        
    }),
    actions: {
        setParam(params){
            Object.assign(this.param,params);
            console.log('workflowUrl changed',this.param);
            this.changed = true;
        },
        //生成展示查看流程的url
        createShowWorkFlowUrl() {
            if (this.workflowUrl && !this.changed) return this.workflowUrl;
            const { isSummary } = globalData;
            const {affair, currentUser, workflow, other, summary, superviseData} =
              useBaseInfoStore().baseInfo.businessData;
            //处理页面等于这个
            let { hasTemplate, scene, caseId } = workflow;
            //新建页面这三个参数另外取
            if (!isSummary) {
                const { workflowButton } = useBaseInfoStore().baseInfo.workflowInfo;
                hasTemplate = workflowButton.isTemplate;
                caseId = workflowButton.caseId;
                //老页面是这样判断的
                if (hasTemplate) {
                    if (workflowButton.isView) {
                        scene = 2;
                    } else {
                        globalMethods.message.error($.i18n('jquery.comp.compThis.workflowEdit'));
                    }
                } else {
                    //接口计算了是否能编辑流程，如果是回退这类特殊的不能编辑流程
                    scene = workflowButton.canEditWorkflow ? 1 : 3;
                }
            }
            let startUser={
                id:'',
                name:'',
                senderAccountName:''
            };
            //待发且是发起人才传入
            if (affair.state === 1 && affair.senderId === currentUser.id){
                startUser = {...currentUser};
            }
            //督办多出来的编辑流程
            const {supervisorIds = ''} = superviseData || {};
            if (supervisorIds.includes(currentUser.id) && affair.state!==1 && !summary?.finishDate){
                window.showEditWorkflowBtn = true;
            }
            const v = globalMethods.getSecurityByUserId();
            const {SD,CWID,currentNodeId}=this.param;
            const currentNodeIdParam=currentNodeId?currentNodeId:(isSummary ? affair.activityId||'' : '');
            this.changed = false;
            const canExePrediction =globalData.isCap4 && (other?.hasWorkflowPredictionPanel)//流程预测
            //拼接URL
            return (this.workflowUrl = `${
                globalData.ctxPath
            }/workflow/designer.do?method=showDiagram&isDebugger=false&isModalDialog=true&isTemplate=${hasTemplate}&formApp=&appName=${
                affair.appEnumStr || other.moduleTypeName
            }&san=&processId=${workflow.processId || workflow.processTemplateId}&currentUserId=${
              startUser.id
            }&currentUserName=${encodeURIComponent(
              startUser.name
            )}&currentUserAccountName=${encodeURIComponent(
              startUser.senderAccountName
            )}&defaultPolicyId=&defaultPolicyName=&flowPermAccountId=&wendanId=-1&isvalidate=false&oldPTId=&startFormViewOperation=undefined&nodeFormViewOperation=undefined&needValidate=undefined&canCopyFlow=undefined&isHistoryFlag=false&caseId=${caseId? caseId||'' : ''}&currentNodeId=${
              currentNodeIdParam
            }&formMutilOprationIds=&showHastenButton=${workflow.showHastenButton? workflow.showHastenButton : false}&superviseId=&selectPeopleExtParams=&SPK=freeFlow&NPS=default&masterId=&canExePrediction=${canExePrediction}&needClonedProcessId=&CWID=${CWID}&SD=${SD}&isSkipNode=false&canSetCircleBranch=true&v=${v}&isCIPModel=false&onlyEditSubProcess=false&disableEditSubprocess=false&action=1&openFromIframe=true&scene=${scene}`).replace(
                /[\r\n\s]/gim,
                ''
            );
        },
        //生成编辑流程的url
        createEditWorkFlowUrl() {
            if (this.workflowUrl) return this.workflowUrl;
            const { summary, currentUser, nodePolicy, workflow } =
                useBaseInfoStore().baseInfo.businessData;
            const v = globalMethods.getSecurityByUserId();
            return (this.workflowUrl = `${globalData.ctxPath}/workflow/designer.do?
            method=showDiagram
            &isDebugger=false
            &isModalDialog=true
            &isTemplate=false
            &formApp=
            &appName=collaboration
            &san=
            &processId=${workflow.processId || workflow.processTemplateId}
            &currentUserId=${currentUser.id}
            &currentUserName=${encodeURIComponent(currentUser.name)}
            &currentUserAccountName=${encodeURIComponent(currentUser.senderAccountName)}
            &defaultPolicyId=${encodeURIComponent(nodePolicy.defaultNodeName)}
            &defaultPolicyName=${encodeURIComponent(nodePolicy.defaultNodeLable)}
            &flowPermAccountId=${currentUser.loginAccount}
            &wendanId=-1
            &accessFileSecretLevel=${getAccessFileSecretLevel()}
            &isvalidate=false
            &oldPTId=
            &startFormViewOperation=undefined
            &nodeFormViewOperation=undefined
            &needValidate=undefined
            &canCopyFlow=undefined
            &isHistoryFlag=false
            &caseId=
            &currentNodeId=
            &formMutilOprationIds=
            &showHastenButton=false
            &superviseId=
            &selectPeopleExtParams=
            &SPK=freeFlow
            &NPS=default
            &masterId=
            &canExePrediction=false
            &needClonedProcessId=
            &CWID=undefined
            &SD=undefined
            &isSkipNode=false
            &canSetCircleBranch=true
            &v=${v}
            &isCIPModel=false
            &onlyEditSubProcess=false
            &disableEditSubprocess=false
            &action=1
            &scene=1`.replace(/[\r\n\s]/gim, ''));
        },
    },
});

//在vue上下文之外使用
export function useSrcCacheWithOut() {
    return useSrcCacheStore(pinia);
}
